package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.PolicyMapper;
import com.heima.mapper.VmPolicyMapper;
import com.heima.pojo.Policy;
import com.heima.pojo.dto.PolicyDto;
import com.heima.pojo.dto.PolicyPageDto;
import com.heima.pojo.dto.VmPolicyPageDto;
import com.heima.pojo.vo.PolicyPageVo;
import com.heima.pojo.vo.VmPolicyVo;
import com.heima.pojo.vo.VmUsePolicyPageVo;
import com.heima.pojo.vo.VmUsePolicyVo;
import com.heima.service.PolicyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@Slf4j
public class PolicyServiceImpl implements PolicyService {

    @Autowired
    private PolicyMapper policyMapper;
    @Autowired
    private VmPolicyMapper vmPolicyMapper;



    /**
     * 分页查询
     * @param dto
     * @return
     */
    @Override
    public PolicyPageVo pageQuery(PolicyPageDto dto) {
        dto.check();
        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());
        Page<Policy> policies =  policyMapper.pageQuery(dto);
        PolicyPageVo policyPageVo = new PolicyPageVo();
        policyPageVo.setCurrentPageRecords(policies);
        policyPageVo.setPageIndex(dto.getPageIndex());
        policyPageVo.setPageSize(dto.getPageSize());
        policyPageVo.setTotalPage(policies.getPages());
        policyPageVo.setTotalCount(policies.getTotal());
        return policyPageVo;
    }

    /**
     * 删除策略
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delById(Integer id) {
        //删除策略
        policyMapper.delById(id);
        //删除应用该策略的设备优惠
        vmPolicyMapper.delByPolicyId(id);
    }

    /**
     * 取消策略
     * @param policyId
     * @param innerCode
     */
    @Override
    public void save(Integer policyId, String innerCode) {
        //删除对应设备编号的策略
        vmPolicyMapper.delByPolicyIdAndInnerCode(policyId,innerCode);
    }

    /**
     * 新增策略
     * @param dto
     */
    @Override
    public void add(PolicyDto dto) {
        Policy policy = new Policy();
        BeanUtils.copyProperties(dto,policy);
        log.info("policy:{}",policy);
        policy.setCreateTime(LocalDateTime.now());
        policy.setUpdateTime(LocalDateTime.now());
        policyMapper.insert(policy);
    }

    /**
     * 根据售货机查询策略
     * @param innerCode
     * @return
     */
    @Override
    public VmPolicyVo queryVmPolicy(String innerCode) {
        VmPolicyVo vmPolicyVo = vmPolicyMapper.queryVmPolicy(innerCode);
        if (vmPolicyVo == null){
            throw new RuntimeException("该售货机没有优惠");
        }
        return vmPolicyVo;
    }

    /**
     * 根据策略查询售货机
     * @param dto
     * @return
     */
    @Override
    public VmUsePolicyPageVo pageQueryByPolicyId(VmPolicyPageDto dto) {
        dto.check();
        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());
        Page<VmUsePolicyVo> pages = policyMapper.pageQueryByPolicyId(dto.getPolicyId());
        VmUsePolicyPageVo vmUsePolicyPageVo = new VmUsePolicyPageVo();
        vmUsePolicyPageVo.setCurrentPageRecords(pages);
        vmUsePolicyPageVo.setPageIndex(dto.getPageIndex());
        vmUsePolicyPageVo.setPageSize(dto.getPageSize());
        vmUsePolicyPageVo.setTotalPage(pages.getPages());
        vmUsePolicyPageVo.setTotalCount(pages.getTotal());
        return vmUsePolicyPageVo;
    }

    /**
     * 修改策略
     * @param dto
     */
    @Override
    public void update(PolicyDto dto) {
        Policy policy = new Policy();
        BeanUtils.copyProperties(dto,policy);
        policyMapper.update(policy);
    }
}
